package org.bmi.gwt.examples.serverevent.server;

import org.apache.log4j.Logger;
import org.bmi.gwt.examples.serverevent.shared.MyTestObject;
import org.bmi.gwt.mi.server.annotation.ClassMiHandler;
import org.bmi.gwt.mi.server.events.MiEvent;
import org.bmi.gwt.mi.server.events.MiEventHandler;
import org.bmi.gwt.mi.shared.exceptions.InvalidArgumentException;

@ClassMiHandler(objectClass = MyTestObject.class, parameterTypes = { String.class }, allowedQueues = { "org.bmi.gwt.mi.test.event" })
public class ConstructorHandler implements MiEventHandler
{
	/*
	 * This class is an implementation of MiEventHandler. Annotations provides
	 * the class, parameters types of the handled method invocation, and allowed
	 * queues. Note that no method name has been provided, this should tell the
	 * handler that only the constructor with provided arguments should be
	 * handled.
	 */
	static protected Logger logger = Logger.getLogger(ConstructorHandler.class);

	@Override
	public void onBeforeMethod(MiEvent event)
	{
		try
		{
			String arg = event.getMethodArgument(0);
			logger.info("constructor argument: " + arg);

			/*
			 * The event is caught and used to replace value which was provided
			 * to the constructor.
			 */

			event.setMethodArgument(arg + " #changed by server!", 0);
		}
		catch (InvalidArgumentException e)
		{
			logger.error("an error occurred in ConstructorHandler:", e);
		}
	}

	@Override
	public void onAfterMethod(MiEvent event)
	{}
}
